<?php
/**
 * Name: 消息控制器.
 * User: 董坤鸿
 * Date: 2016/10/12
 * Time: 14:25
 */

namespace App\Http\Controllers\Resources;
use App\Plugins\Wechat\Messages;
use App\Services\WxClientService;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MessagesController extends Controller
{

    /**
     * 初始化
     * MessagesController constructor.
     */
    public function __construct()
    {
        $this->_repoService = new WxClientService();
    }

    /**
     * 群发消息列表
     * @return mixed
     */
    public function groups()
    {
        $messageGroupsList = $this->_repoService->getMessageGroupsList();
        return $messageGroupsList;
    }

    /**
     * 获取消息
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (request()->has('time')) {
            $time = request()->input('time');
            return $this->_repoService->getMessages($time);
        }else{
            return $this->_repoService->getMessages();
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 发送消息
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $message = $request->all();
        $res = $this->_repoService->checkMessage($message);
        $ret = 0;
        if ($res) {
            if (!empty($message['isGroup']) && $message['isGroup'] == true) {
                foreach ($res as $v) {
                    $messageHandler = new Messages($v);
                    $result = $messageHandler->send();
                    sleep(0.01);
                    $ret = ($ret or $result);
                }
            } else {
                $messageHandler = new Messages($res);
                $ret = $messageHandler->send();
            }
            if ($ret) {
                return '';
            }
            $this->_error('发送失败');
        }
        return $this->_error('发送失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
